package com.team.star.controller;


import com.team.star.entity.dto.RoleDTO;
import com.team.star.entity.vo.RoleVO;
import com.team.star.service.RoleService;
import com.team.star.utils.ResponseCode;
import com.team.star.utils.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "角色管理")
@RestController
@RequestMapping("/role")
public class RoleController {
    private final RoleService roleService;

    @Autowired
    public RoleController(RoleService roleService) {
        this.roleService = roleService;
    }

    @GetMapping("/list")
    @Operation(summary = "获取角色列表")
    public Result<List<RoleDTO>> getRoleList() {
        List<RoleDTO> roleList = roleService.getRoleList();
        return Result.success(ResponseCode.SUCCESS, roleList);
    }

    @GetMapping("/{roleName}")
    @Operation(summary = "获取角色信息")
    public Result<RoleDTO> getRole(@PathVariable String roleName) {
        RoleDTO role = roleService.getRoleByName(roleName);
        return Result.success(ResponseCode.SUCCESS, role);
    }

    @PostMapping("/addRole")
    @Operation(summary = "添加角色")
    public Result<Void> addRole(@RequestBody RoleVO roleVO) {
        roleService.addRole(roleVO);
        return Result.success(ResponseCode.SUCCESS);
    }

    @DeleteMapping("/{roleName}")
    @Operation(summary = "删除角色")
    public Result<Void> deleteRole(@PathVariable String roleName) {
        roleService.deleteRole(roleName);
        return Result.success(ResponseCode.SUCCESS);
    }

    @PutMapping("/updateRole")
    @Operation(summary = "更新角色信息")
    public Result<Void> updateRole(@RequestBody RoleVO roleVO) {
        roleService.updateRole(roleVO);
        return Result.success(ResponseCode.SUCCESS);
    }

}
